package org.itli.wacp.Service.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.itli.wacp.Mapper.UserMapper;
import org.itli.wacp.Service.EmailService;
import org.itli.wacp.Service.UserService;
import org.itli.wacp.Util.MD5Util;
import org.itli.wacp.Util.YZMUntil;
import org.itli.wacp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;
    private RedisTemplate redisTemplate;
    private EmailService emailService;
    String Email;

    @Autowired
    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User Login(String username, String password) {
        Map<String,Object> claim = new HashMap<>();
        MD5Util md5 = new MD5Util();
        username = md5.encrypt(username);
        password = md5.encrypt(password);
        log.info("用户名MD5"+username);
        log.info("密码,MD5"+password);
        Map<String,String> map = new HashMap<>();
        map.put("id",username);
        map.put("password",password);
        User login = userMapper.Login(map);
        return login;
    }

    @Override
    public int Regist(User user) {
        String id = user.getId();
        String password = user.getPassword();
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        map.put("password",password);

        MD5Util md5 = new MD5Util();
        User logins = userMapper.Login(map);
        System.out.println(logins);

        if(userMapper.Login(map) == null){
            id = md5.encrypt(id);
            password = md5.encrypt(password);
            user.setId(id);
            user.setPassword(password);

            int regist = userMapper.Regist(user);
            return regist;
        }
        else {
            return 404;
        }
    }

    @Override
    public String Member(String id) {
        String member = userMapper.Member(id);
        return member;
    }

    @Override
    public String JHSendEmail(String username, String Email) {
        this.Email = Email;
        //需求：添加邮箱并验证，验证成功后方可通过！
        YZMUntil yzm = new YZMUntil();
        String s = yzm.generateCode();
        //发送邮箱
        emailService.sendSimpleMessage(Email,"激活您的账户","天马科技：您的验证码为"+s+"，一分钟后验证码自动失效。若非您本人操作，请联系本平台管理员，感谢您的支持");
        //存入redis中
        redisTemplate.opsForValue().set(username+"YZM",s);
        redisTemplate.expire(username+"YZM",1,TimeUnit.MINUTES);
        return "验证码发送成功！";
    }

    @Override
    public String JHJYEmail(String username, String YZM) {
        String OKYZM = (String) redisTemplate.opsForValue().get(username + "YZM");
        if(YZM.equals(OKYZM)){
            int i = userMapper.UpdateEmail(username, Email);
            if(i==1){
                return "激活成功!";
            }
            else {
                return "异常，请重试！";
            }
        }
        else {
            return "验证码错误或过期！";
        }
    }

    @Override
    public String JY(String username) {
        User found = userMapper.Found(username);
        if(found.getEmail() == null){
            return "-1";
        }
        else {
            return "1";
        }
    }

    @Override
    public String GetEmail(String username) {
        String s = userMapper.GetEmail(username);
        return s;
    }

    @Override
    public String CPSendYZM(String username,String email) {
        //需求：添加邮箱并验证，验证成功后方可通过！
        YZMUntil yzm = new YZMUntil();
        String s = yzm.generateCode();
        System.out.println(s);
        //发送邮箱
        emailService.sendSimpleMessage(email,"修改密码","天马科技：您的验证码为"+s+"，一分钟后验证码自动失效。若非您本人操作，请联系本平台管理员，感谢您的支持");
        //存入Redis
        redisTemplate.opsForValue().set(username+"EYZM",s);
        redisTemplate.expire(username+"EYZM",1,TimeUnit.MINUTES);
        return "发送成功！";
    }

    @Override
    public String CPJYYZM(String username, String YZM) {
        String OKYZM = (String) redisTemplate.opsForValue().get(username + "EYZM");
        if(OKYZM.equals(YZM)){
            return "身份验证成功！";
        }
        else{
            return "验证码错误或超时，请重新申请";
        }
    }

    @Override
    public String ChangePassword(String id, String password) {
        MD5Util md5 = new MD5Util();
        String md5pwd = md5.encrypt(password);
        int i = userMapper.ChangePassword(id, md5pwd);
        if(i==1){
            //删除预留在缓存中的令牌
            Boolean delete = redisTemplate.opsForValue().getOperations().delete(id + "token");
            return "更新成功，请重新登陆！";
        }
        else {
            return "更新失败，请重试";
        }
    }


}
